package com.major.search.binary;

public class Binary {

    public static int binary(int[] arr, int val){
        return binary(arr, val, 0, arr.length - 1);
    }

    public static int binary(int[] arr, int value, int left, int right) {
        int mid = (left + right) / 2;
        if (left >= right && arr[mid] != value) {
            return -1;
        }
        // 向右
        else if (arr[mid] < value) {
            return binary(arr, value, mid + 1, right);
        }
        // 向左
        else if (arr[mid] > value) {
            return binary(arr, value, left, mid - 1);
        }
        // 判断中间值，始终找到，中断递归
        else {
            return mid;
        }
    }
}
